package com.koron.configure.workflow.controller;


import com.github.pagehelper.PageInfo;
import com.koron.bean.app.AppBean;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.workflow.bean.po.ConfigureWorkflow;
import com.koron.common.core.business.workflow.bean.properties.WorkFlowConfigProperties;
import com.koron.common.core.business.workflow.bean.properties.WorkflowPlugin;
import com.koron.common.core.business.workflow.bean.properties.WorkflowProperties;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.configure.workflow.bean.dto.ConfigureWorkflowDTO;
import com.koron.configure.workflow.service.ConfigureWorkflowService;
import com.koron.configure.workflow.service.impl.WorkflowEngineService;
import com.koron.configure.workflow.service.impl.WorkflowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 业务配置管理-流程配置(ConfigureWorkflow)表控制层
 *
 * @author jzp
 * @since 2022-01-11 11:35:49
 */
@Api(tags = "流程配置")
@RestController
@RequestMapping("configureWorkflow")
public class ConfigureWorkflowController {
    /**
     * 服务对象
     */
    @Autowired
    private ConfigureWorkflowService configureWorkflowService;

    @Autowired
    private WorkflowService workflowService;

    @Autowired
    private WorkFlowConfigProperties workFlowConfigProperties;

    @Autowired
    private WorkflowEngineService workflowEngineService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<PageInfo> page(PageQuery pageQuery, ConfigureWorkflow query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), configureWorkflowService, "page", PageInfo.class, pageQuery, query));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<ConfigureWorkflowDTO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), configureWorkflowService, "getDtoById", ConfigureWorkflowDTO.class, id));
    }

    @ApiOperation(value = "根据processCode查询")
    @GetMapping("getByProcessCode")
    public Response<ConfigureWorkflow> selectOneByProcessCode(String processCode) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), configureWorkflowService, "getWorkflow", ConfigureWorkflow.class, processCode));
    }

    @ApiOperation(value = "保存或更新数据")
    @PostMapping
    public Response<String> save(@RequestBody ConfigureWorkflowDTO configureWorkflowDTO) {
        ADOConnection.runTask(getCurrDs(), configureWorkflowService, "saveDto", Boolean.class, configureWorkflowDTO);
        return Response.ok(configureWorkflowDTO.getId());
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), configureWorkflowService, "deleteById", Boolean.class, id);
        return Response.ok();
    }

    @GetMapping("inner/workflowProperties")
    public Response<WorkflowProperties> getWorkflowProperties(String processCode) {
        return Response.ok(workflowService.getWorkflowProperties(processCode));
    }

    @GetMapping("inner/workflowPlugin")
    public Response<WorkflowPlugin> getPluginUrlByNode(String processCode, String nodeCode, Integer type) {
        return Response.ok(workflowService.getPluginUrlByNode(processCode, nodeCode, type));
    }

    /**
     * 获取工作流引擎链接
     */
    @ApiOperation(value = "获取工作流引擎链接")
    @GetMapping("href")
    public Response<String> href() {
        return Response.ok(workFlowConfigProperties.getUrl());
    }

    /**
     * 获取工作流模板组字典
     */
    @ApiOperation(value = "获取工作流模板组字典")
    @GetMapping("setCodes")
    public Response<List<ConfigureWorkflow>> setCodes(@ApiParam("租户编码") @RequestParam(required = true) String appCode) {
        return Response.ok(workflowEngineService.listSetCode(appCode));
    }

    /**
     * 获取工作流模板字典
     */
    @ApiOperation(value = "获取工作流模板字典")
    @GetMapping("processCodes")
    public Response<List<ConfigureWorkflow>> processCodes(@ApiParam("工作流模板组id") @RequestParam(required = true) String setId) {
        return Response.ok(workflowEngineService.listProcessCode(setId));
    }

    /**
     * 获取工作流模板字典
     */
    @ApiOperation(value = "将当前租户所有流程的应用编号都修改为当前租户的应用编号",tags = "慎用")
    @GetMapping("updateDefaultAppCode")
    public Response<String> updateDefaultAppCode() {
        String code = Optional.ofNullable(ThreadLocalContext.getApp()).map(AppBean::getCode).orElseThrow(() -> new IllegalArgumentException("当前租户编码为空"));
        ADOConnection.runTask(getCurrDs(), configureWorkflowService, "updateDefaultAppCode", Void.class, code);
        return Response.ok();
    }

    @ApiOperation(value = "从选定租户中复制所有的流程配置",tags = "慎用")
    @GetMapping("copyWorkflowFromAppCode")
    public Response<String> copyWorkflowFromAppCode(String code) {
        ADOConnection.runTask(getCurrDs(), configureWorkflowService, "copyWorkflowFromAppCode", Void.class, code);
        return Response.ok();
    }

}
